package com.qiaoanju.web.back.controller.system;

import com.qiaoanju.back.domain.system.GroupModel;
import com.qiaoanju.back.service.system.IGroupService;
import com.qiaoanju.commons.constants.ErrorCodes;
import com.qiaoanju.commons.constants.SessionConstant;
import com.qiaoanju.commons.constants.StringConstant;
import com.qiaoanju.commons.enums.ResultCode;
import com.qiaoanju.commons.utils.DateUtil;
import com.qiaoanju.web.controller.BaseController;
import com.qiaoanju.web.security.service.LoginUserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/sysgroup")
public class GroupController extends BaseController {
	
	@Autowired
	private IGroupService service ;

	@RequestMapping(value = "/index", method = { RequestMethod.GET })
	public String list(Model model, HttpServletRequest request,
                       HttpServletResponse response) throws Exception {

		//获取登录用户的信息
		model.addAttribute(SessionConstant.SESSION_USER, LoginUserService.getLoginUser());
		List<GroupModel> groups = service.getAllGroups(0);
		model.addAttribute("mainTreelists",groups);

		List<GroupModel> firstGroups = service.getChildsByRoot(0);
		model.addAttribute("list",firstGroups);

		return "sys/group/list";

	}

	@RequestMapping(value = "/editModel", method = { RequestMethod.GET})
	@ResponseBody
	public void editModel(Model model, HttpServletRequest request,
						  HttpServletResponse response, int id){
		int result = 0 ;
		if(id != 0){
			GroupModel group = service.getGroupById(id);
			//处理结果
			if (group != null){
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("data", group);
				jsonObject.put("msg", StringConstant.FORMAT_DATA_SUCCESS_MSG);
				printJson(response,ResultCode.Success,jsonObject);
			}else{
				printJson(response, ErrorCodes.PARAM_ERROR, StringConstant.PARAM_ERROR_MSG);
			}
		}else{
			printJson(response,ErrorCodes.PARAM_NULL, StringConstant.PARAM_NULL_MSG);
		}

	}


	@RequestMapping(value = "/edit", method = { RequestMethod.POST})
	@ResponseBody
	public void edit(Model model, HttpServletRequest request,
					 HttpServletResponse response, GroupModel group){
		boolean result = false;
		if(group != null && group.getId() != 0){
			GroupModel editModel = service.getGroupById(group.getId());

			group.setUpdate_id(LoginUserService.getLoginUserId());
			group.setUpdate_time(DateUtil.getCurrentDate());
			result = service.edit(group);
			//处理结果
			if (result){
				printJson(response, ResultCode.Success, StringConstant.UPDATE_MSG);
			}else{
				printJson(response,ResultCode.Fail, StringConstant.OPERATE_FAILURE_MSG);
			}
		}else{
			printJson(response,ErrorCodes.PARAM_ERROR, StringConstant.PARAM_ERROR_MSG);
		}

	}

}
